package com.terrorism.sdk.utils;

import java.util.regex.Pattern;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.util.Log;
import android.util.Patterns;

import com.terrorism.sdk.R;

/**
 * @since Monday, June 02, 2014
 * @author hoangcaomobile
 *
 */
public class TextUtils {

	// account and password
	public static boolean isAccountPasswordOk(String text) {
		boolean isOk = true;
		if (text.matches("[a-z0-9_]*")) {
			isOk = true;
		} else {
			isOk = false;
		}
		return isOk;
	}
	// --

	// check if string is int
	public static boolean isInt(String string) {  
		try {  
			int i = Integer.parseInt(string);
			Log.i("TextUtils", "is int: " + Integer.toString(i));
		}  
		catch(NumberFormatException nfe) {  
			return false;  
		}  
		return true;  
	}
	// --

	// check if string is null
	public static boolean isStringNull(String str) {
		if (str == null || str.equals(""))
			return true;
		return false;
	}

	// -- 

	// get email
	public static String getEmail(Context context) {
		Pattern emailPattern = Patterns.EMAIL_ADDRESS;
		Account[] accounts = AccountManager.get(context).getAccounts();
		for (Account account : accounts) {
			if (emailPattern.matcher(account.name).matches()) {
				if (account.name.contains("@gmail.com") && account.type.contains("com.google")) {
					return account.name;
				}
			}
		}
		return "";
	}
	// --

	// get app name
	public static String getApplicationName(Context mContext) {
		String appName = mContext.getString(R.string.textviewGreatApp);
		try {
			int stringId = mContext.getApplicationInfo().labelRes;
			appName = mContext.getString(stringId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return appName;
	}
	// --

}
